package com.eii.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eii.common.annotation.LoginUser;
import com.eii.common.model.auth.SysUser;
import com.eii.common.web.controller.BaseController;
import com.eii.common.web.domain.Result;
import com.eii.common.web.page.PageResult;
import com.eii.excel.util.ExcelUtils;
import com.eii.log.annotation.AuditLog;
import com.eii.oauth2.common.annotation.RequiresPermissions;
import com.eii.oauth2.common.util.SecurityUtils;
import com.eii.system.api.domain.SysDictData;
import com.eii.system.controller.vo.SysDictDataExcel;
import com.eii.system.service.ISysDictDataService;
import com.eii.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController {

    private final static String CONTROLLER_TITLE = "字典数据";

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public PageResult list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getPageResult(list);
    }

    @AuditLog(title = CONTROLLER_TITLE, operation = "导出")
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData) throws IOException {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        List<SysDictDataExcel> sysDictDataExcels = BeanUtil.copyToList(list, SysDictDataExcel.class);
        String datestr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String filename = "字典数据" + datestr + ".xlsx";
        ExcelUtils.exportExcelStream(response, filename, SysDictDataExcel.class, sysDictDataExcels);
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public Result getInfo(@PathVariable Long dictCode) {
        return Result.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public Result dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (data == null) {
            data = new ArrayList<SysDictData>();
        }
        return Result.success(data);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @AuditLog(title = CONTROLLER_TITLE, operation = "新增")
    @PostMapping
    public Result add(@Validated @RequestBody SysDictData dict, @LoginUser SysUser user) {
        dict.setCreateBy(user.getUsername());
        return toResult(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @AuditLog(title = CONTROLLER_TITLE, operation = "新增")
    @PutMapping
    public Result edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toResult(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @AuditLog(title = CONTROLLER_TITLE, operation = "删除")
    @DeleteMapping("/{dictCodes}")
    public Result remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return Result.success();
    }
}
